﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class SchoolSeedForm : Form
    {
        int regID, avpID, fhID, pfshID = -1, cultID, seedID;
        tb_schoolseed schoolSeed;

        public SchoolSeedForm()
        {
            InitializeComponent();
        }

        private void SchoolSeedForm_Load(object sender, EventArgs e)
        {
            cbRegion.SelectedIndexChanged -= cbRegion_SelectedIndexChanged;
            cbRegion.DataSource = UtilClass.FillCBRegions();
            cbRegion.DisplayMember = "Name";
            cbRegion.ValueMember = "ID";

            if (InformationClass.isAdmin)
            {
                cbRegion.SelectedIndex = -1;
                cbRegion.Text = string.Empty;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
            }
            else
            {
                cbRegion.Enabled = false;
                regID = InformationClass.regionID;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
                cbRegion.SelectedValue = regID;
                cbRegion_SelectedIndexChanged(cbRegion, null);
                cbAVP.Enabled = true;
                cbAVP.Text = string.Empty;
                //toolStrip1.Visible = false;
            }

            cbCulture.SelectedIndexChanged -= cbCulture_SelectedIndexChanged;
            cbCulture.DataSource = UtilClass.GetCBCultureDataSourse();
            cbCulture.DisplayMember = "Name";
            cbCulture.ValueMember = "ID";
            cbCulture.SelectedIndex = -1;
            cbCulture.SelectedIndexChanged += new EventHandler(cbCulture_SelectedIndexChanged);
        }

        private void cbRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbRegion.SelectedIndex >= 0)
            {
                cbAVP.Enabled = true;
                regID = Convert.ToInt32(cbRegion.SelectedValue);
                cbAVP.SelectedIndexChanged -= cbAVP_SelectedIndexChanged;
                cbAVP.DataSource = UtilClass.FillAVP(regID);
                cbAVP.ValueMember = "ID";
                cbAVP.DisplayMember = "Name";
                cbAVP.SelectedIndex = -1;
                cbAVP.Text = string.Empty;
                cbFH.DataSource = null;
                cbPFSH.DataSource = null;
                cbAVP.SelectedIndexChanged += new EventHandler(cbAVP_SelectedIndexChanged);

            }
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAVP.SelectedIndex >= 0)
            {
                avpID = Convert.ToInt32(cbAVP.SelectedValue);
                cbFH.SelectedIndexChanged -= cbFH_SelectedIndexChanged;
                cbFH.Enabled = true;
                cbFH.DataSource = DBClass.Farms
                    .Where(wh => wh.AVPID == avpID)
                    .Select(se => new { ID = se.ID, Name = se.Name })
                    .OrderBy(o => o.Name)
                    .ToArray();
                cbFH.DisplayMember = "Name";
                cbFH.ValueMember = "ID";
                cbFH.SelectedIndex = -1;
                cbFH.Text = string.Empty;
                cbPFSH.DataSource = null;
                cbFH.SelectedIndexChanged += new EventHandler(cbFH_SelectedIndexChanged);
            }
        }

        private void cbFH_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbFH.SelectedIndex >= 0)
            {
                fhID = Convert.ToInt32(cbFH.SelectedValue);
                cbPFSH.Enabled = true;
                cbPFSH.SelectedIndexChanged -= cbPFSH_SelectedIndexChanged;
                cbPFSH.DataSource = DBClass.Schools
                    .Where(wh => wh.FarmID == fhID)
                    .Select(se => new { ID = se.ID, Name = se.Name })
                    .OrderBy(o => o.Name)
                    .ToArray();
                cbPFSH.DisplayMember = "Name";
                cbPFSH.ValueMember = "ID";
                cbPFSH.SelectedIndex = -1;
                cbPFSH.Text = string.Empty;
                cbPFSH.SelectedIndexChanged += new EventHandler(cbPFSH_SelectedIndexChanged);
            }
        }

        private void cbPFSH_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbPFSH.SelectedIndex >= 0)
            {
                pfshID = Convert.ToInt32(cbPFSH.SelectedValue);
                groupBox1.Enabled = true;
                if (seedID > 0 && pfshID > 0)
                    ShowSchoolSeed(seedID, pfshID, dtpDate.Value.Date);
            }
            else
            {
                pfshID = 0;
                groupBox1.Enabled = false;
            }
        }

        private void ShowSchoolSeed(int seedID, int pfshID, DateTime dateTime)
        {
            schoolSeed = DBClass.SchoolSeeds
                .Where(wh => wh.SchoolID == pfshID && wh.Seed == seedID && wh.Date == dateTime)
                .SingleOrDefault();
            if (schoolSeed != null)
            {
                tbArea.Text = schoolSeed.Area.Value.ToString();
                tbCost.Text = schoolSeed.Cost.Value.ToString();
                tbQuantity.Text = schoolSeed.Quantity.Value.ToString();
            }
            else
            {
                tbQuantity.Text = tbCost.Text = tbArea.Text = string.Empty;
            }
        }

        private void cbCulture_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbCulture.SelectedIndex >= 0)
            {
                cultID = Convert.ToInt32(cbCulture.SelectedValue);
                cbSeed.SelectedIndexChanged -= cbSeed_SelectedIndexChanged;
                cbSeed.DataSource = InformationClass.Lang ?
                    DBClass.Seeds
                    .Where(wh => wh.CultureID == cultID)
                    .Select(se => new
                    {
                        ID = se.ID,
                        Name = se.Name
                    }).ToArray() :
                    DBClass.Seeds
                    .Where(wh => wh.CultureID == cultID)
                    .Select(se => new
                    {
                        ID = se.ID,
                        Name = se.NameUz
                    }).ToArray();
                cbSeed.DisplayMember = "Name";
                cbSeed.ValueMember = "ID";
                cbSeed.SelectedIndex = -1;
                cbSeed.Text = string.Empty;
                cbSeed.SelectedIndexChanged += new EventHandler(cbSeed_SelectedIndexChanged);
            }
        }

        private void cbSeed_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbSeed.SelectedIndex >= 0)
            {
                seedID = Convert.ToInt32(cbSeed.SelectedValue);
                if (seedID > 0 && pfshID > 0)
                    ShowSchoolSeed(seedID, pfshID, dtpDate.Value.Date);
            }
            else
                seedID = 0;
        }

        private void dtpDate_ValueChanged(object sender, EventArgs e)
        {
            if (seedID > 0 && pfshID > 0)
                ShowSchoolSeed(seedID, pfshID, dtpDate.Value.Date);
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (pfshID > 0 && seedID > 0)
            {
                schoolSeed = DBClass.SchoolSeeds
                    .Where(wh => wh.Date == dtpDate.Value.Date && wh.Seed == seedID && wh.SchoolID == pfshID)
                    .SingleOrDefault();
                decimal area, opCost, hQuant;

                if (tbArea.Text.GetDecimalOrDefault( out area) &&
                tbCost.Text.GetDecimalOrDefault( out opCost) &&
                tbQuantity.Text.GetDecimalOrDefault(out hQuant))
                {

                    if (schoolSeed != null)
                    {
                        schoolSeed.Area = area;
                        schoolSeed.Cost = opCost;
                        schoolSeed.Quantity = hQuant;
                    }
                    else
                    {
                        schoolSeed = new tb_schoolseed
                        {
                            Area = area,
                            Seed = seedID,
                            Date = dtpDate.Value.Date,
                            Quantity = hQuant,
                            SchoolID = pfshID,
                            Cost = opCost
                        };
                        DBClass.SchoolSeeds.InsertOnSubmit(schoolSeed);
                    }
                    try
                    {
                        DBClass.RESPContext.SubmitChanges();
                        MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
                    }
                    catch
                    {
                        MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
                    }
                }
            }
            else
            {
                if (InformationClass.Lang)
                    MessageBox.Show("Не верно заполненны данные. Проверьте правильность заполнения.");
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
